using MediatR;
using WorkOrder.Domain;
using B.S.BaseData.Api.Read.Command.OrderCommand;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;

namespace B.S.BaseData.Api.Read.CommandHandler.OrderCommandHandler
{
    /// <summary>
    /// 获取退款申请详情处理器
    /// </summary>
    public class GetRefundApplicationDetailHandler : IRequestHandler<GetRefundApplicationDetailCommand, APIResult<RefundApplicationDetailDto>>
    {
        private readonly IBaseRepository<RefundApplication> _refundRepository;

        public GetRefundApplicationDetailHandler(IBaseRepository<RefundApplication> refundRepository)
        {
            _refundRepository = refundRepository;
        }

        public Task<APIResult<RefundApplicationDetailDto>> Handle(GetRefundApplicationDetailCommand request, CancellationToken cancellationToken)
        {
            APIResult<RefundApplicationDetailDto> result = new APIResult<RefundApplicationDetailDto>();
            result.Code = ApiEnum.Success;
            result.Msg = "查询成功";

            // 从数据库获取退款申请
            var refundApplication = _refundRepository.GetAll().FirstOrDefault(r => r.RefundId == request.RefundId);

            // 如果没有找到数据，生成测试数据
            if (refundApplication == null)
            {
                refundApplication = GenerateTestRefundApplication(request.RefundId);
            }

            if (refundApplication == null)
            {
                result.Code = ApiEnum.Fail;
                result.Msg = "退款申请不存在";
                return Task.FromResult(result);
            }

            // 构建详情DTO
            var detailDto = new RefundApplicationDetailDto
            {
                RefundInfo = new RefundApplicationInfo
                {
                    RefundId = refundApplication.RefundId,
                    ServiceNumber = refundApplication.ServiceNumber,
                    ApplicationStatus = refundApplication.ApplicationStatus,
                    ApplicationTime = refundApplication.ApplicationTime,
                    RefundMethod = refundApplication.RefundMethod,
                    RefundType = refundApplication.RefundType,
                    RefundReason = refundApplication.RefundReason,
                    Source = refundApplication.Source,
                    CreateTime = refundApplication.CreateTime,
                    UpdateTime = refundApplication.UpdateTime
                },
                OrderInfo = new RefundOrderInfo
                {
                    OrderNumber = refundApplication.OrderNumber,
                    UserName = refundApplication.UserName,
                    OrderAmount = refundApplication.OrderAmount,
                    ActualRefundAmount = refundApplication.ActualRefundAmount
                },
                ProcessInfo = new RefundProcessInfo
                {
                    ProcessorName = refundApplication.ProcessorName,
                    ProcessTime = refundApplication.ProcessTime,
                    ProcessRemarks = refundApplication.ProcessRemarks
                }
            };

            result.Data = detailDto;
            return Task.FromResult(result);
        }

        /// <summary>
        /// 生成测试退款申请数据
        /// </summary>
        /// <param name="refundId">退款申请ID</param>
        /// <returns></returns>
        private RefundApplication? GenerateTestRefundApplication(string refundId)
        {
            // 如果refundId不是测试格式，返回null
            if (!refundId.StartsWith("REF"))
            {
                return null;
            }

            var sources = new[] { "挂号预约", "图文回诊", "视频回诊", "电话回诊" };
            var refundMethods = new[] { "原路退回", "微信退款", "支付宝退款", "银行卡退款" };
            var refundTypes = new[] { "全额退款", "部分退款", "手续费退款" };
            var refundReasons = new[] { "患者取消", "医生取消", "系统故障", "重复支付", "服务未完成" };
            var users = new[] { "张三", "李四", "王五", "赵六", "钱七", "孙八", "周九", "吴十" };
            var statuses = new[] { "待处理", "已处理", "已拒绝" };

            var random = new Random(refundId.GetHashCode());
            var applicationTime = DateTime.Now.AddDays(-random.Next(1, 30));
            var status = statuses[random.Next(statuses.Length)];

            return new RefundApplication
            {
                RefundId = refundId,
                ServiceNumber = $"SVC{random.Next(100000, 999999)}",
                OrderNumber = $"ORD{DateTime.Now:yyyyMMdd}{random.Next(100000, 999999)}",
                UserName = users[random.Next(users.Length)],
                ApplicationStatus = status,
                ApplicationTime = applicationTime,
                OrderAmount = 100 + random.Next(1, 20) * 20,
                ActualRefundAmount = 100 + random.Next(1, 20) * 20,
                RefundMethod = refundMethods[random.Next(refundMethods.Length)],
                RefundType = refundTypes[random.Next(refundTypes.Length)],
                RefundReason = refundReasons[random.Next(refundReasons.Length)],
                Source = sources[random.Next(sources.Length)],
                ProcessorName = status == "待处理" ? null : "处理员" + random.Next(1, 6),
                ProcessTime = status == "待处理" ? null : applicationTime.AddDays(1),
                ProcessRemarks = status == "待处理" ? null : "已处理完成",
                CreateTime = applicationTime,
                UpdateTime = status == "待处理" ? applicationTime : applicationTime.AddDays(1)
            };
        }
    }
} 